package org.ntu.csn.doraemon.canvas;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class StartMenu extends MIDlet implements CommandListener {
	
	private static String [] gameList = { "New Game", "Game Rule", "Rankings"};
	
    private static StartMenu instance = null;
    
	public static final List mainMenu = new List("Doraemon", Choice.IMPLICIT,gameList, null);
	
	private DescForm mainForm;
	
	private DiffForm diff;
	
	private FailForm ff;
	
	private RankingForm rf;
	
	public StartMenu() {
		instance = this;
	}

	public static StartMenu getInstance(){
		return instance;
	}
	
	protected void startApp() throws MIDletStateChangeException {
		Display display;
		mainMenu.addCommand(Listener.exit);
		mainMenu.setCommandListener(this);
		display = Display.getDisplay(this);
		display.setCurrent(mainMenu);
	}

	public void display()
    {
        Display.getDisplay(this).setCurrent(mainMenu);
    }
	
    public void commandAction(Command c, Displayable d)
    {
         if (c == List.SELECT_COMMAND)
         {        	 
            switch (((List)d).getSelectedIndex()) {
                case 0:
                	diff = new DiffForm(this);
                	Display.getDisplay(this).setCurrent(diff.getDiff());
                    break;
                case 1:
                	mainForm = new DescForm(this);
                	Display.getDisplay(this).setCurrent(mainForm.getdescForm());
                    break;
                case 2:
                	rf = new RankingForm(this);
                	Display.getDisplay(this).setCurrent(rf.getRankingForm());
                    break;
                case 3:
                	ff = new FailForm(this);
                	Display.getDisplay(this).setCurrent(ff.getMsgForm());
                	break;
                 }
         } else if ( c == Listener.exit ) {
			try {
				quit();
			} catch (MIDletStateChangeException e) {
				e.printStackTrace();
			}
         }
    }

	private void quit() throws MIDletStateChangeException {
		destroyApp(true);
        notifyDestroyed();

	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
	}

	protected void pauseApp() {
	}


}
